{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from client import IncoreClient \n",
    "from fragilityservice import FragilityService\n",
    "from mappingset import MappingSet\n",
    "from bridgedamage import BridgeDamage\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Connection successful to IN-CORE services. pyIncore version detected: 1.4.1\n"
     ]
    }
   ],
   "source": [
    "# Connect to IN-CORE service\n",
    "client = IncoreClient()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Bridge damage for Shelby county, TN with New Madrid earthquake."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# New Madrid earthquake using Atkinson Boore 1995\n",
    "hazard_type = \"earthquake\"\n",
    "hazard_id = \"5b902cb273c3371e1236b36b\"\n",
    "\n",
    "# Bridget inventory in Shelby county, TN\n",
    "bridge_dataset_id = \"5a284f2dc7d30d13bc082040\"\n",
    "\n",
    "# Default Bridge Fragility Mapping\n",
    "mapping_id = \"5b47bcce337d4a37755e0cb2\"\n",
    "\n",
    "# Use hazard uncertainty for computing damage\n",
    "use_hazard_uncertainty = False\n",
    "# Use liquefaction (LIQ) column of bridges to modify fragility curve\n",
    "use_liquefaction = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataset already exists locally. Reading from local cached zip.\n",
      "Unzipped folder found in the local cache. Reading from it...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create bridge damage \n",
    "bridge_dmg = BridgeDamage(client)\n",
    "\n",
    "# Load input dataset\n",
    "bridge_dmg.load_remote_input_dataset(\"bridges\", bridge_dataset_id)\n",
    "\n",
    "# Load fragility mapping\n",
    "fragility_service = FragilityService(client)\n",
    "mapping_set = MappingSet(fragility_service.get_mapping(mapping_id))\n",
    "bridge_dmg.set_input_dataset(\"dfr3_mapping_set\", mapping_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Specify the result name\n",
    "result_name = \"bridge_result\"\n",
    "\n",
    "# Set analysis parameters\n",
    "bridge_dmg.set_parameter(\"result_name\", result_name)\n",
    "bridge_dmg.set_parameter(\"hazard_type\", hazard_type)\n",
    "bridge_dmg.set_parameter(\"hazard_id\", hazard_id)\n",
    "bridge_dmg.set_parameter(\"num_cpu\", 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Unable to calculate limit state. Evaluation failed for expression: \n[{\"condition\": [\"one_sec_SA > 0\"], \"expression\": \"scipy.stats.norm.cdf((math.log(one_sec_SA) - math.log(0.6 * (0.85 if liq.upper() == 'U' else 0.65 if liq.upper() == 'Y' else 1)))/(0.6))\"}]\nProvided Inputs: \n{\"1.0 sec SA\": 1.1499999762}\n{}",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31m_RemoteTraceback\u001b[0m                          Traceback (most recent call last)",
      "\u001b[1;31m_RemoteTraceback\u001b[0m: \n\"\"\"\nTraceback (most recent call last):\n  File \"k:\\ana\\envs\\pyincoreEnv\\lib\\concurrent\\futures\\process.py\", line 243, in _process_worker\n    r = call_item.fn(*call_item.args, **call_item.kwargs)\n  File \"k:\\ana\\envs\\pyincoreEnv\\lib\\concurrent\\futures\\process.py\", line 202, in _process_chunk\n    return [fn(*args) for args in chunk]\n  File \"k:\\ana\\envs\\pyincoreEnv\\lib\\concurrent\\futures\\process.py\", line 202, in <listcomp>\n    return [fn(*args) for args in chunk]\n  File \"k:\\1111111\\INCORE\\die\\bridgedamage.py\", line 198, in bridge_damage_analysis_bulk_input\n    selected_fragility_set.calculate_limit_state(hval_dict,\n  File \"k:\\1111111\\INCORE\\die\\fragilitycurveset.py\", line 98, in calculate_limit_state\n    probability = fragility_curve.solve_curve_expression(hazard_values,\n  File \"k:\\1111111\\INCORE\\die\\dfr3curve.py\", line 106, in solve_curve_expression\n    raise ValueError(error_msg)\nValueError: Unable to calculate limit state. Evaluation failed for expression: \n[{\"condition\": [\"one_sec_SA > 0\"], \"expression\": \"scipy.stats.norm.cdf((math.log(one_sec_SA) - math.log(0.6 * (0.85 if liq.upper() == 'U' else 0.65 if liq.upper() == 'Y' else 1)))/(0.6))\"}]\nProvided Inputs: \n{\"1.0 sec SA\": 1.1499999762}\n{}\n\"\"\"",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32mk:\\1111111\\INCORE\\die\\bridge_dmg.ipynb Cell 7'\u001b[0m in \u001b[0;36m<cell line: 2>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      <a href='vscode-notebook-cell:/k%3A/1111111/INCORE/die/bridge_dmg.ipynb#ch0000006?line=0'>1</a>\u001b[0m \u001b[39m# Run bridge damage analysis\u001b[39;00m\n\u001b[1;32m----> <a href='vscode-notebook-cell:/k%3A/1111111/INCORE/die/bridge_dmg.ipynb#ch0000006?line=1'>2</a>\u001b[0m bridge_dmg\u001b[39m.\u001b[39;49mrun_analysis()\n",
      "File \u001b[1;32mk:\\1111111\\INCORE\\die\\baseanalysis.py:265\u001b[0m, in \u001b[0;36mBaseAnalysis.run_analysis\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    <a href='file:///k%3A/1111111/INCORE/die/baseanalysis.py?line=261'>262</a>\u001b[0m         \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mError reading parameter: \u001b[39m\u001b[39m\"\u001b[39m \u001b[39m+\u001b[39m result[\u001b[39m1\u001b[39m])\n\u001b[0;32m    <a href='file:///k%3A/1111111/INCORE/die/baseanalysis.py?line=262'>263</a>\u001b[0m         \u001b[39mreturn\u001b[39;00m result\n\u001b[1;32m--> <a href='file:///k%3A/1111111/INCORE/die/baseanalysis.py?line=264'>265</a>\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mrun()\n",
      "File \u001b[1;32mk:\\1111111\\INCORE\\die\\bridgedamage.py:63\u001b[0m, in \u001b[0;36mBridgeDamage.run\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=58'>59</a>\u001b[0m     inventory_args\u001b[39m.\u001b[39mappend(\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=59'>60</a>\u001b[0m         inventory_list[count:count \u001b[39m+\u001b[39m avg_bulk_input_size])\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=60'>61</a>\u001b[0m     count \u001b[39m+\u001b[39m\u001b[39m=\u001b[39m avg_bulk_input_size\n\u001b[1;32m---> <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=62'>63</a>\u001b[0m (ds_results, damage_results) \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mbridge_damage_concurrent_future(\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=63'>64</a>\u001b[0m     \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mbridge_damage_analysis_bulk_input, num_workers,\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=64'>65</a>\u001b[0m     inventory_args, repeat(hazard_type),\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=65'>66</a>\u001b[0m     repeat(hazard_dataset_id))\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=67'>68</a>\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mset_result_csv_data(\u001b[39m\"\u001b[39m\u001b[39mresult\u001b[39m\u001b[39m\"\u001b[39m, ds_results, name\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mget_parameter(\u001b[39m\"\u001b[39m\u001b[39mresult_name\u001b[39m\u001b[39m\"\u001b[39m))\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=68'>69</a>\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mset_result_json_data(\u001b[39m\"\u001b[39m\u001b[39mmetadata\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=69'>70</a>\u001b[0m                           damage_results,\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=70'>71</a>\u001b[0m                           name\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mget_parameter(\u001b[39m\"\u001b[39m\u001b[39mresult_name\u001b[39m\u001b[39m\"\u001b[39m) \u001b[39m+\u001b[39m \u001b[39m\"\u001b[39m\u001b[39m_additional_info\u001b[39m\u001b[39m\"\u001b[39m)\n",
      "File \u001b[1;32mk:\\1111111\\INCORE\\die\\bridgedamage.py:91\u001b[0m, in \u001b[0;36mBridgeDamage.bridge_damage_concurrent_future\u001b[1;34m(self, function_name, num_workers, *args)\u001b[0m\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=88'>89</a>\u001b[0m output_dmg \u001b[39m=\u001b[39m []\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=89'>90</a>\u001b[0m \u001b[39mwith\u001b[39;00m concurrent\u001b[39m.\u001b[39mfutures\u001b[39m.\u001b[39mProcessPoolExecutor(max_workers\u001b[39m=\u001b[39mnum_workers) \u001b[39mas\u001b[39;00m executor:\n\u001b[1;32m---> <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=90'>91</a>\u001b[0m     \u001b[39mfor\u001b[39;00m ret1, ret2 \u001b[39min\u001b[39;00m executor\u001b[39m.\u001b[39mmap(function_name, \u001b[39m*\u001b[39margs):\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=91'>92</a>\u001b[0m         output_ds\u001b[39m.\u001b[39mextend(ret1)\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=92'>93</a>\u001b[0m         output_dmg\u001b[39m.\u001b[39mextend(ret2)\n",
      "File \u001b[1;32mk:\\ana\\envs\\pyincoreEnv\\lib\\concurrent\\futures\\process.py:559\u001b[0m, in \u001b[0;36m_chain_from_iterable_of_lists\u001b[1;34m(iterable)\u001b[0m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=552'>553</a>\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_chain_from_iterable_of_lists\u001b[39m(iterable):\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=553'>554</a>\u001b[0m     \u001b[39m\"\"\"\u001b[39;00m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=554'>555</a>\u001b[0m \u001b[39m    Specialized implementation of itertools.chain.from_iterable.\u001b[39;00m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=555'>556</a>\u001b[0m \u001b[39m    Each item in *iterable* should be a list.  This function is\u001b[39;00m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=556'>557</a>\u001b[0m \u001b[39m    careful not to keep references to yielded objects.\u001b[39;00m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=557'>558</a>\u001b[0m \u001b[39m    \"\"\"\u001b[39;00m\n\u001b[1;32m--> <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=558'>559</a>\u001b[0m     \u001b[39mfor\u001b[39;00m element \u001b[39min\u001b[39;00m iterable:\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=559'>560</a>\u001b[0m         element\u001b[39m.\u001b[39mreverse()\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=560'>561</a>\u001b[0m         \u001b[39mwhile\u001b[39;00m element:\n",
      "File \u001b[1;32mk:\\ana\\envs\\pyincoreEnv\\lib\\concurrent\\futures\\_base.py:608\u001b[0m, in \u001b[0;36mExecutor.map.<locals>.result_iterator\u001b[1;34m()\u001b[0m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=604'>605</a>\u001b[0m \u001b[39mwhile\u001b[39;00m fs:\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=605'>606</a>\u001b[0m     \u001b[39m# Careful not to keep a reference to the popped future\u001b[39;00m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=606'>607</a>\u001b[0m     \u001b[39mif\u001b[39;00m timeout \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n\u001b[1;32m--> <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=607'>608</a>\u001b[0m         \u001b[39myield\u001b[39;00m fs\u001b[39m.\u001b[39;49mpop()\u001b[39m.\u001b[39;49mresult()\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=608'>609</a>\u001b[0m     \u001b[39melse\u001b[39;00m:\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=609'>610</a>\u001b[0m         \u001b[39myield\u001b[39;00m fs\u001b[39m.\u001b[39mpop()\u001b[39m.\u001b[39mresult(end_time \u001b[39m-\u001b[39m time\u001b[39m.\u001b[39mmonotonic())\n",
      "File \u001b[1;32mk:\\ana\\envs\\pyincoreEnv\\lib\\concurrent\\futures\\_base.py:445\u001b[0m, in \u001b[0;36mFuture.result\u001b[1;34m(self, timeout)\u001b[0m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=442'>443</a>\u001b[0m     \u001b[39mraise\u001b[39;00m CancelledError()\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=443'>444</a>\u001b[0m \u001b[39melif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_state \u001b[39m==\u001b[39m FINISHED:\n\u001b[1;32m--> <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=444'>445</a>\u001b[0m     \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m__get_result()\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=445'>446</a>\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=446'>447</a>\u001b[0m     \u001b[39mraise\u001b[39;00m \u001b[39mTimeoutError\u001b[39;00m()\n",
      "File \u001b[1;32mk:\\ana\\envs\\pyincoreEnv\\lib\\concurrent\\futures\\_base.py:390\u001b[0m, in \u001b[0;36mFuture.__get_result\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=387'>388</a>\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_exception:\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=388'>389</a>\u001b[0m     \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m--> <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=389'>390</a>\u001b[0m         \u001b[39mraise\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_exception\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=390'>391</a>\u001b[0m     \u001b[39mfinally\u001b[39;00m:\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=391'>392</a>\u001b[0m         \u001b[39m# Break a reference cycle with the exception in self._exception\u001b[39;00m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=392'>393</a>\u001b[0m         \u001b[39mself\u001b[39m \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m\n",
      "\u001b[1;31mValueError\u001b[0m: Unable to calculate limit state. Evaluation failed for expression: \n[{\"condition\": [\"one_sec_SA > 0\"], \"expression\": \"scipy.stats.norm.cdf((math.log(one_sec_SA) - math.log(0.6 * (0.85 if liq.upper() == 'U' else 0.65 if liq.upper() == 'Y' else 1)))/(0.6))\"}]\nProvided Inputs: \n{\"1.0 sec SA\": 1.1499999762}\n{}"
     ]
    }
   ],
   "source": [
    "# Run bridge damage analysis\n",
    "bridge_dmg.run_analysis()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Retrieve result dataset\n",
    "result = bridge_dmg.get_output_dataset(\"result\")\n",
    "\n",
    "# Convert dataset to Pandas DataFrame\n",
    "df = result.get_dataframe_from_csv()\n",
    "\n",
    "# Display top 5 rows of output data\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Bridge damage for Seaside, OR with an earthquake."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataset already exists locally. Reading from local cached zip.\n",
      "Unzipped folder found in the local cache. Reading from it...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Load input dataset and fragility mapping\n",
    "bridge_dmg.load_remote_input_dataset(\"bridges\", \"5d6ede5db9219c34b56fc20b\")\n",
    "mapping_set = MappingSet(fragility_service.get_mapping(\"5d55c3a1b9219c0689f1f898\"))\n",
    "bridge_dmg.set_input_dataset(\"dfr3_mapping_set\", mapping_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Unable to calculate limit state. Evaluation failed for expression: \n[{\"condition\": [\"one_sec_SA > 0\"], \"expression\": \"scipy.stats.norm.cdf((math.log(one_sec_SA) - math.log(0.6 * (0.85 if liq.upper() == 'U' else 0.65 if liq.upper() == 'Y' else 1)))/(0.6))\"}]\nProvided Inputs: \n{\"1.0 sec SA\": 1.1499999762}\n{}",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31m_RemoteTraceback\u001b[0m                          Traceback (most recent call last)",
      "\u001b[1;31m_RemoteTraceback\u001b[0m: \n\"\"\"\nTraceback (most recent call last):\n  File \"k:\\ana\\envs\\pyincoreEnv\\lib\\concurrent\\futures\\process.py\", line 243, in _process_worker\n    r = call_item.fn(*call_item.args, **call_item.kwargs)\n  File \"k:\\ana\\envs\\pyincoreEnv\\lib\\concurrent\\futures\\process.py\", line 202, in _process_chunk\n    return [fn(*args) for args in chunk]\n  File \"k:\\ana\\envs\\pyincoreEnv\\lib\\concurrent\\futures\\process.py\", line 202, in <listcomp>\n    return [fn(*args) for args in chunk]\n  File \"k:\\1111111\\INCORE\\die\\bridgedamage.py\", line 198, in bridge_damage_analysis_bulk_input\n    selected_fragility_set.calculate_limit_state(hval_dict,\n  File \"k:\\1111111\\INCORE\\die\\fragilitycurveset.py\", line 98, in calculate_limit_state\n    probability = fragility_curve.solve_curve_expression(hazard_values,\n  File \"k:\\1111111\\INCORE\\die\\dfr3curve.py\", line 106, in solve_curve_expression\n    raise ValueError(error_msg)\nValueError: Unable to calculate limit state. Evaluation failed for expression: \n[{\"condition\": [\"one_sec_SA > 0\"], \"expression\": \"scipy.stats.norm.cdf((math.log(one_sec_SA) - math.log(0.6 * (0.85 if liq.upper() == 'U' else 0.65 if liq.upper() == 'Y' else 1)))/(0.6))\"}]\nProvided Inputs: \n{\"1.0 sec SA\": 1.1499999762}\n{}\n\"\"\"",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32mk:\\1111111\\INCORE\\die\\bridge_dmg.ipynb Cell 11'\u001b[0m in \u001b[0;36m<cell line: 5>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      <a href='vscode-notebook-cell:/k%3A/1111111/INCORE/die/bridge_dmg.ipynb#ch0000010?line=2'>3</a>\u001b[0m bridge_dmg\u001b[39m.\u001b[39mset_parameter(\u001b[39m\"\u001b[39m\u001b[39mhazard_id\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39m5ba8f379ec2309043520906f\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m      <a href='vscode-notebook-cell:/k%3A/1111111/INCORE/die/bridge_dmg.ipynb#ch0000010?line=3'>4</a>\u001b[0m bridge_dmg\u001b[39m.\u001b[39mset_parameter(\u001b[39m\"\u001b[39m\u001b[39mnum_cpu\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m4\u001b[39m)\n\u001b[1;32m----> <a href='vscode-notebook-cell:/k%3A/1111111/INCORE/die/bridge_dmg.ipynb#ch0000010?line=4'>5</a>\u001b[0m bridge_dmg\u001b[39m.\u001b[39;49mrun_analysis()\n",
      "File \u001b[1;32mk:\\1111111\\INCORE\\die\\baseanalysis.py:265\u001b[0m, in \u001b[0;36mBaseAnalysis.run_analysis\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    <a href='file:///k%3A/1111111/INCORE/die/baseanalysis.py?line=261'>262</a>\u001b[0m         \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mError reading parameter: \u001b[39m\u001b[39m\"\u001b[39m \u001b[39m+\u001b[39m result[\u001b[39m1\u001b[39m])\n\u001b[0;32m    <a href='file:///k%3A/1111111/INCORE/die/baseanalysis.py?line=262'>263</a>\u001b[0m         \u001b[39mreturn\u001b[39;00m result\n\u001b[1;32m--> <a href='file:///k%3A/1111111/INCORE/die/baseanalysis.py?line=264'>265</a>\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mrun()\n",
      "File \u001b[1;32mk:\\1111111\\INCORE\\die\\bridgedamage.py:63\u001b[0m, in \u001b[0;36mBridgeDamage.run\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=58'>59</a>\u001b[0m     inventory_args\u001b[39m.\u001b[39mappend(\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=59'>60</a>\u001b[0m         inventory_list[count:count \u001b[39m+\u001b[39m avg_bulk_input_size])\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=60'>61</a>\u001b[0m     count \u001b[39m+\u001b[39m\u001b[39m=\u001b[39m avg_bulk_input_size\n\u001b[1;32m---> <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=62'>63</a>\u001b[0m (ds_results, damage_results) \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mbridge_damage_concurrent_future(\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=63'>64</a>\u001b[0m     \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mbridge_damage_analysis_bulk_input, num_workers,\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=64'>65</a>\u001b[0m     inventory_args, repeat(hazard_type),\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=65'>66</a>\u001b[0m     repeat(hazard_dataset_id))\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=67'>68</a>\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mset_result_csv_data(\u001b[39m\"\u001b[39m\u001b[39mresult\u001b[39m\u001b[39m\"\u001b[39m, ds_results, name\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mget_parameter(\u001b[39m\"\u001b[39m\u001b[39mresult_name\u001b[39m\u001b[39m\"\u001b[39m))\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=68'>69</a>\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mset_result_json_data(\u001b[39m\"\u001b[39m\u001b[39mmetadata\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=69'>70</a>\u001b[0m                           damage_results,\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=70'>71</a>\u001b[0m                           name\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mget_parameter(\u001b[39m\"\u001b[39m\u001b[39mresult_name\u001b[39m\u001b[39m\"\u001b[39m) \u001b[39m+\u001b[39m \u001b[39m\"\u001b[39m\u001b[39m_additional_info\u001b[39m\u001b[39m\"\u001b[39m)\n",
      "File \u001b[1;32mk:\\1111111\\INCORE\\die\\bridgedamage.py:91\u001b[0m, in \u001b[0;36mBridgeDamage.bridge_damage_concurrent_future\u001b[1;34m(self, function_name, num_workers, *args)\u001b[0m\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=88'>89</a>\u001b[0m output_dmg \u001b[39m=\u001b[39m []\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=89'>90</a>\u001b[0m \u001b[39mwith\u001b[39;00m concurrent\u001b[39m.\u001b[39mfutures\u001b[39m.\u001b[39mProcessPoolExecutor(max_workers\u001b[39m=\u001b[39mnum_workers) \u001b[39mas\u001b[39;00m executor:\n\u001b[1;32m---> <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=90'>91</a>\u001b[0m     \u001b[39mfor\u001b[39;00m ret1, ret2 \u001b[39min\u001b[39;00m executor\u001b[39m.\u001b[39mmap(function_name, \u001b[39m*\u001b[39margs):\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=91'>92</a>\u001b[0m         output_ds\u001b[39m.\u001b[39mextend(ret1)\n\u001b[0;32m     <a href='file:///k%3A/1111111/INCORE/die/bridgedamage.py?line=92'>93</a>\u001b[0m         output_dmg\u001b[39m.\u001b[39mextend(ret2)\n",
      "File \u001b[1;32mk:\\ana\\envs\\pyincoreEnv\\lib\\concurrent\\futures\\process.py:559\u001b[0m, in \u001b[0;36m_chain_from_iterable_of_lists\u001b[1;34m(iterable)\u001b[0m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=552'>553</a>\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_chain_from_iterable_of_lists\u001b[39m(iterable):\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=553'>554</a>\u001b[0m     \u001b[39m\"\"\"\u001b[39;00m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=554'>555</a>\u001b[0m \u001b[39m    Specialized implementation of itertools.chain.from_iterable.\u001b[39;00m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=555'>556</a>\u001b[0m \u001b[39m    Each item in *iterable* should be a list.  This function is\u001b[39;00m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=556'>557</a>\u001b[0m \u001b[39m    careful not to keep references to yielded objects.\u001b[39;00m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=557'>558</a>\u001b[0m \u001b[39m    \"\"\"\u001b[39;00m\n\u001b[1;32m--> <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=558'>559</a>\u001b[0m     \u001b[39mfor\u001b[39;00m element \u001b[39min\u001b[39;00m iterable:\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=559'>560</a>\u001b[0m         element\u001b[39m.\u001b[39mreverse()\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/process.py?line=560'>561</a>\u001b[0m         \u001b[39mwhile\u001b[39;00m element:\n",
      "File \u001b[1;32mk:\\ana\\envs\\pyincoreEnv\\lib\\concurrent\\futures\\_base.py:608\u001b[0m, in \u001b[0;36mExecutor.map.<locals>.result_iterator\u001b[1;34m()\u001b[0m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=604'>605</a>\u001b[0m \u001b[39mwhile\u001b[39;00m fs:\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=605'>606</a>\u001b[0m     \u001b[39m# Careful not to keep a reference to the popped future\u001b[39;00m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=606'>607</a>\u001b[0m     \u001b[39mif\u001b[39;00m timeout \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n\u001b[1;32m--> <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=607'>608</a>\u001b[0m         \u001b[39myield\u001b[39;00m fs\u001b[39m.\u001b[39;49mpop()\u001b[39m.\u001b[39;49mresult()\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=608'>609</a>\u001b[0m     \u001b[39melse\u001b[39;00m:\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=609'>610</a>\u001b[0m         \u001b[39myield\u001b[39;00m fs\u001b[39m.\u001b[39mpop()\u001b[39m.\u001b[39mresult(end_time \u001b[39m-\u001b[39m time\u001b[39m.\u001b[39mmonotonic())\n",
      "File \u001b[1;32mk:\\ana\\envs\\pyincoreEnv\\lib\\concurrent\\futures\\_base.py:445\u001b[0m, in \u001b[0;36mFuture.result\u001b[1;34m(self, timeout)\u001b[0m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=442'>443</a>\u001b[0m     \u001b[39mraise\u001b[39;00m CancelledError()\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=443'>444</a>\u001b[0m \u001b[39melif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_state \u001b[39m==\u001b[39m FINISHED:\n\u001b[1;32m--> <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=444'>445</a>\u001b[0m     \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m__get_result()\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=445'>446</a>\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=446'>447</a>\u001b[0m     \u001b[39mraise\u001b[39;00m \u001b[39mTimeoutError\u001b[39;00m()\n",
      "File \u001b[1;32mk:\\ana\\envs\\pyincoreEnv\\lib\\concurrent\\futures\\_base.py:390\u001b[0m, in \u001b[0;36mFuture.__get_result\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=387'>388</a>\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_exception:\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=388'>389</a>\u001b[0m     \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m--> <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=389'>390</a>\u001b[0m         \u001b[39mraise\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_exception\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=390'>391</a>\u001b[0m     \u001b[39mfinally\u001b[39;00m:\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=391'>392</a>\u001b[0m         \u001b[39m# Break a reference cycle with the exception in self._exception\u001b[39;00m\n\u001b[0;32m    <a href='file:///k%3A/ana/envs/pyincoreEnv/lib/concurrent/futures/_base.py?line=392'>393</a>\u001b[0m         \u001b[39mself\u001b[39m \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m\n",
      "\u001b[1;31mValueError\u001b[0m: Unable to calculate limit state. Evaluation failed for expression: \n[{\"condition\": [\"one_sec_SA > 0\"], \"expression\": \"scipy.stats.norm.cdf((math.log(one_sec_SA) - math.log(0.6 * (0.85 if liq.upper() == 'U' else 0.65 if liq.upper() == 'Y' else 1)))/(0.6))\"}]\nProvided Inputs: \n{\"1.0 sec SA\": 1.1499999762}\n{}"
     ]
    }
   ],
   "source": [
    "bridge_dmg.set_parameter(\"result_name\", \"seaside_eq_bridge_dmg\")\n",
    "bridge_dmg.set_parameter(\"hazard_type\", \"earthquake\")\n",
    "bridge_dmg.set_parameter(\"hazard_id\", \"5ba8f379ec2309043520906f\")\n",
    "bridge_dmg.set_parameter(\"num_cpu\", 4)\n",
    "bridge_dmg.run_analysis()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read result dataset\n",
    "df = pd.read_csv(\"seaside_eq_bridge_dmg.csv\")\n",
    "\n",
    "# Display top 5 rows of output data\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Bridge damage for Seaside, OR with a tsunami."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Load input dataset and fragility mapping\n",
    "bridge_dmg.load_remote_input_dataset(\"bridges\", \"5d6ede5db9219c34b56fc20b\")\n",
    "mapping_set = MappingSet(fragility_service.get_mapping(\"5d275000b9219c3c553c7202\"))\n",
    "bridge_dmg.set_input_dataset(\"dfr3_mapping_set\", mapping_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "bridge_dmg.set_parameter(\"result_name\", \"seaside_tsu_bridge_dmg\")\n",
    "bridge_dmg.set_parameter(\"hazard_type\", \"tsunami\")\n",
    "bridge_dmg.set_parameter(\"hazard_id\", \"5d27b986b9219c3c55ad37d0\")\n",
    "bridge_dmg.set_parameter(\"num_cpu\", 4)\n",
    "bridge_dmg.run_analysis()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Bridge damage for Galveston, TX with Hurricane"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hazard_type = \"hurricane\"\n",
    "hazard_id = \"5f15cd627db08c2ccc4e3bab\"\n",
    "\n",
    "# Galveston bridge\n",
    "bridge_dataset_id = \"60620320be94522d1cb9f7f0\"\n",
    "\n",
    "# Galveston hurricane bridge mapping\n",
    "refactored_mapping_id = \"6062254b618178207f66226c\"\n",
    "\n",
    "# Create bridge damage\n",
    "bridge_dmg = BridgeDamage(client)\n",
    "\n",
    "# Load input datasets\n",
    "bridge_dmg.load_remote_input_dataset(\"bridges\", bridge_dataset_id)\n",
    "\n",
    "# Load fragility mapping\n",
    "fragility_service = FragilityService(client)\n",
    "refactored_mapping_set = MappingSet(fragility_service.get_mapping(refactored_mapping_id))\n",
    "bridge_dmg.set_input_dataset('dfr3_mapping_set', refactored_mapping_set)\n",
    "\n",
    "# Set analysis parameters\n",
    "bridge_dmg.set_parameter(\"fragility_key\", \"Hurricane SurgeLevel and WaveHeight Fragility ID Code\")\n",
    "bridge_dmg.set_parameter(\"result_name\", \"galveston_bridge_dmg_result\")\n",
    "bridge_dmg.set_parameter(\"hazard_type\", hazard_type)\n",
    "bridge_dmg.set_parameter(\"hazard_id\", hazard_id)\n",
    "bridge_dmg.set_parameter(\"num_cpu\", 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run bridge damage analysis\n",
    "bridge_dmg.run_analysis()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = bridge_dmg.get_output_dataset(\"result\")\n",
    "df = result.get_dataframe_from_csv()\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "65f8545d5b179584e4344a378342d3091d162296694f191a9d66db391bbacd9c"
  },
  "kernelspec": {
   "display_name": "Python 3.9.9 ('pyincoreEnv')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
